﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CachedServices;
using Core.DBInterfaces;
using Core.Logging;
using Microsoft.Practices.ServiceLocation;
using Core.Email;
using Core.Store;
using Core.WebScaner;
using Core.Extended;

namespace Infrastructure
{
    public abstract class BaseManager : IDisposable
    {        
        protected IDbRepository _dbContext;   
        protected ILogger _logger;
        protected IEmailer _emailer;
        protected ICacheProvider _cache;
        protected IWebCrawler _webCrawler;

        public BaseManager()
        {
            _dbContext = ServiceLocator.Current.GetInstance<IDbRepository>();
            _logger = ServiceLocator.Current.GetInstance<ILogger>();
            _cache = ServiceLocator.Current.GetInstance<ICacheProvider>();
            _emailer = ServiceLocator.Current.GetInstance<IEmailer>();
            _webCrawler = ServiceLocator.Current.GetInstance<IWebCrawler>();
        }

        public IEnumerable<Culture> GetCultures()
        {
            return _dbContext.Get<Culture>();
        }

        internal List<C> ToExtendedClassObjectList<T, C>(List<T> classObjectList, string lang)
            where T : class
            where C : class, IExtended<T, C>, new()
        {
            if (classObjectList == null)
                throw new ArgumentNullException("Object list is null");

            var extendedList = new List<C>();

            foreach (var classObject in classObjectList)
            {
                extendedList.Add(ToExtendedClassObject<T, C>(classObject, lang));
            }

            return extendedList;
        }

        internal C ToExtendedClassObject<T, C>(T classObject, string lang)
            where T : class
            where C : class, IExtended<T, C>, new()
        {
            if (classObject == null)
                throw new ArgumentNullException("Object is null");

            return new C().MapParentClassObject(classObject, lang);
        }

        public void Dispose()
        {
            if (_dbContext != null)
            {
                _dbContext.Dispose();
                _dbContext = null;
            }
        }        

    }
}
